#include "connectionreader.h"
namespace apollo
{
	namespace core
	{
		ConnectionReader::ConnectionReader(QSharedPointer<QAbstractSocket> socket, ConcurrentQueue<PayloadPtr> * queue,QSharedPointer<WireFormat> format)
			:socket(socket), queue(queue),format(format)
		{
		}

		ConnectionReader::~ConnectionReader(void)
		{
		}

		void ConnectionReader::run()
		{
			while(!isStop)
			{
				if(socket->state() != QAbstractSocket::ConnectedState)
				{
					QThread::sleep(20);
				}
				else if(socket->bytesAvailable() > 0)
				{
					PayloadPtr payload(new Payload);
					WireFormat::ReadState state = format->read(socket, payload);
					if(state == WireFormat::Successful)
					{
						queue->enqueue(payload);
					}
					else
					{
						//error
					}
				}

			}
		}
	}}
